package phoenix.web.test_objects;

import org.testng.TestException;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.test_objects.data_providers.UserPageDataProviders;

/**
 * Class contains test methods for Index page
 */
public class PaymentRedirectionsTests extends TestBaseWebExt
{
	public PaymentRedirectionsTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	boolean p_expected = true;
	boolean p_actual;

	// Data
	public UserPageDataProviders dataProviders = new UserPageDataProviders(manager);
	MessengerTests messenger = new MessengerTests(manager);
	MainMenuTests menuTests = new MainMenuTests(manager);

	/**
	 * Free => UserPage view Looking for Upgrade banner redirect on Payment page
	 */
	public void lookingForUpgradeBannerTest(TestUserDataModel p_userRecipient)
	{
		try
		{
			wd().openPage("/user/view/id/" + p_userRecipient.getId());
			// Fills up block Looking for at Profile page
			if (manager.isMD())
			{
				wd().scrollDown();
				page().userPage().clickLookingForBlock();
				wd().wait(1);
			}

			page().userPage().clickBannerLookingFor();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: view Looking for Upgrade banner redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("lookingForUpgradeBannerTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => UserPage view full size Primary photo redirect on Payment page
	 */
	public void fullSizePrimaryPhotoTest(TestUserDataModel p_userRecipient)
	{
		try
		{
			wd().openPage("/user/view/id/" + p_userRecipient.getId());
			page().userPage().clickUserPhoto();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: view full size Primary photo redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("fullSizePrimaryPhotoTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => UserPage view full size Additional photo redirect on Payment page
	 */
	public void fullSizeAdditionalPhotoTest(TestUserDataModel p_userRecipient)
	{
		try
		{
			wd().openPage("/user/view/id/" + p_userRecipient.getId());
			page().userPage().clickUserAdditionalPhoto();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: view full size Additional photo redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("fullSizeAdditionalPhotoTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => MyAccountPage button Upgrade redirect on Payment page
	 */
	public void buttonUpgradeTest()
	{
		try
		{
			wd().openPage("/account");
			page().myAccountPage().clickButtonUpgrade();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "MyAccountPage: button Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("buttonUpgradeTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => SearchPage Near banner Upgrade redirect on Payment page
	 */
	public void searchNearBannerUpgradeTest()
	{
		try
		{
			wd().openAutologin(testUser());
			page().searchPage().closeAllPopUps();
			menuTests.searchMenuPhotoQualityTest();

			page().searchPage().scrollToBannerUpgrade();
			page().searchPage().clickBannerUpgrade();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "SearchPage Near: banner Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("searchNearBannerUpgradeTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => SearchPage Online banner Upgrade redirect on Payment page
	 */
	public void searchOnlineBannerUpgradeTest()
	{
		try
		{
			wd().openAutologin(testUser());
			page().searchPage().closeAllPopUps();
			menuTests.searchMenuSearchOnlineTest();

			page().searchPage().scrollToBannerUpgrade();
			page().searchPage().clickBannerUpgrade();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "SearchPage Online: banner Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("searchOnlineBannerUpgradeTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free => SearchPage Recently banner Upgrade redirect on Payment page
	 */
	public void searchRecentlyBannerUpgradeTest()
	{
		try
		{
			wd().openAutologin(testUser());
			page().searchPage().closeAllPopUps();
			menuTests.searchMenuNewMembersTest();

			page().searchPage().scrollToBannerUpgrade();
			page().searchPage().clickBannerUpgrade();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "SearchPage Recently: banner Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("searchRecentlyBannerUpgradeTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}

	/**
	 * Free/Paid => UserPage Free Communication icon Upgrade redirect on payment page
	 */
	public void freeCommunicationIconTest(TestUserDataModel p_userPaidFeatures)
	{
		try
		{
			wd().openAutologin(testUser());
			wd().openPage("/user/view/id/" + p_userPaidFeatures.getId());
			page().userPage().waitForPageLoad();

			if (manager.isMD())
			{
				if (!page().userPage().isFriendsTextPresent())
				{
					page().userPage().clickAddFriends();
					wd().wait(2);

					if (!page().userPage().isFriendsTextPresent())
					{
						log().errorAndScreen("Cannot add to friends");
						return;
					}
				}

				wd().openPage("/favorites");
			}

			page().userPage().clickLinkFreeCommunication();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: Free Communication icon Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("freeCommunicationIconTest: " + p_ex.toString());
		}

		finally
		{
			// Verifying result
			test().forErrors();
		}
	}

	/**
	 * Free/Paid => UserPage Invisible mode icon Upgrade redirect on payment page
	 */
	public void invisibleModeIconTest(TestUserDataModel p_userPaidFeatures)
	{
		try
		{
			wd().openAutologin(testUser());
			wd().openPage("/user/view/id/" + p_userPaidFeatures.getId());
			page().userPage().waitForPageLoad();

			if (manager.isMD())
			{
				if (!page().userPage().isFriendsTextPresent())
				{
					page().userPage().clickAddFriends();
					wd().wait(2);

					if (!page().userPage().isFriendsTextPresent())
					{
						log().errorAndScreen("Cannot add to friends");
						return;
					}
				}

				wd().openPage("/favorites");
			}

			page().userPage().clickLinkInvisibleMode();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: Invisible mode icon Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("invisibleModeIconTest: " + p_ex.toString());
		}

		finally
		{
			// Verifying result
			test().forErrors();
		}
	}

	/**
	 * Free/Paid => UserPage VIP in search icon Upgrade redirect on payment page
	 */
	public void vipInSearchIconTest(TestUserDataModel p_userPaidFeatures)
	{
		try
		{
			wd().openPage("/user/view/id/" + p_userPaidFeatures.getId());
			page().userPage().waitForPageLoad();

			if (manager.isMD())
			{
				if (!page().userPage().isFriendsTextPresent())
				{
					page().userPage().clickAddFriends();
					wd().wait(2);

					if (!page().userPage().isFriendsTextPresent())
					{
						log().errorAndScreen("Cannot add to friends");
						return;
					}
				}

				wd().openPage("/favorites");
			}

			page().userPage().clickLinkVipInSearch();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: VIP in search icon Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("vipInSearchIconTest: " + p_ex.toString());
		}

		finally
		{
			// Verifying result
			test().forErrors();
		}
	}

	/**
	 * Free => UserPage Hot user icon Upgrade redirect on payment page
	 */
	public void hotUserIconTest(TestUserDataModel p_userHot)
	{
		try
		{
			wd().openPage("/user/view/id/" + p_userHot.getId());
			page().userPage().waitForPageLoad();

			if (manager.isMD())
			{
				if (!page().userPage().isFriendsTextPresent())
				{
					page().userPage().clickAddFriends();
					wd().wait(2);

					if (!page().userPage().isFriendsTextPresent())
					{
						log().errorAndScreen("Cannot add to friends");
						return;
					}
				}

				wd().openPage("/favorites");
			}

			page().userPage().clickLinkHotUser();
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "UserPage: Hot user icon Upgrade redirect on Payment page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("hotUserIconTest: " + p_ex.toString());
		}

		finally
		{
			// Verifying result
			test().forErrors();
		}
	}

	/**
	 * Paid => SearchPage TopMenu MessagedMe Activity tool tip redirect on features page
	 */
	public void topMenuDropDownBannerTest(String p_messageKey, TestUserDataModel p_userSender, TestUserDataModel p_userRecipient)
	{
		try
		{
			messenger.sendMessage(p_userRecipient, p_userSender, p_messageKey);
			// Check if message sent
			if (!page().messenger().isMessagePresent(p_messageKey))
			{
				throw new TestException("Unable to send message to " + p_userSender + ", from " + p_userRecipient + ", text: " + p_messageKey);
			}

			// Goes to site
			wd().openURL(manager.testUser().getAutologin());
			page().searchPage().waitForPageLoad();
			page().searchPage().closeAllPopUps();

			// Open messenger from top menu
			page().menuPage().clickTopMenuMessagedMe();
			// Click banner
			page().menuPage().clickCommunicationPopupDropMenu();
			// Test begins
			p_actual = page().paymentPage().isPaymentPage();
			test().assertEquals(p_actual, p_expected, "SearchPage: TopMenu MessagedMe Activity tool tip redirect on Features page");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("topMenuDropDownBunnerTest: " + p_ex.toString());
		}

		// Verifying result
		test().forErrors();
	}
}